package com.studyyoun.android.nonghe.home.item;
/**
 * Created by zhaolong on 2017/10/7.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.ArtDetaileActivity;
import com.studyyoun.android.nonghe.home.model.ArtModel;
import com.studyyoun.android.nonghe.home.sql.ArtSqlDBHelper;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.share.QQShareFunction;
import com.studyyoun.android.nonghe.share.WeixinShareFunction;
import com.studyyoun.android.nonghe.share.WinxinSharModel;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * class infation
 */
public class ItemArtDetaileOtherShareClass {
    private Context mContext;
    private Handler mHandler;
    private ArtModel mArtModel;
    private PopupWindow mSharePopupWindow;
    private final ImageView mAddMyCollectImageView;


    public ItemArtDetaileOtherShareClass(View view, ArtDetaileActivity context, Handler handler, ArtModel artModel) {

        mContext = context;
        mHandler = handler;
        mArtModel = artModel;


        LinearLayout shareWeiXinMsgLayout = context.findViewById(R.id.ll_share_weixin_msg);
        shareWeiXinMsgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin msg share ");
                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                String artName = "《智慧安卓》 " + mArtModel.artName;
                String artFalg = mArtModel.artFalg;

                String artImageUrl = mArtModel.artImageUrl;
                artImageUrl = HttpUrlHelper.BaseHost + artImageUrl;

                String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                WinxinSharModel winxinSharModel = new WinxinSharModel();

                winxinSharModel.webUrl = htmlUrl;
                winxinSharModel.description = artFalg;
                winxinSharModel.title = artName;

                WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.MSG);

            }
        });

        LinearLayout shareWeiXinRingLayout = context.findViewById(R.id.ll_share_weixin_ring);
        shareWeiXinRingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin ring share ");
                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                String artName = "《智慧安卓》 " + mArtModel.artName;
                String artFalg = mArtModel.artFalg;

                String artImageUrl = mArtModel.artImageUrl;
                artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

                String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                WinxinSharModel winxinSharModel = new WinxinSharModel();

                winxinSharModel.webUrl = htmlUrl;
                winxinSharModel.description = artFalg;
                winxinSharModel.title = artName;

                WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.FIND);

            }
        });

        LinearLayout shareQqMsgLayout = context.findViewById(R.id.ll_share_qq_msg);
        shareQqMsgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("qq msg share ");
                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                QQShareFunction.getInstance().shareArtPicAndMsgToQQ((ArtDetaileActivity) mContext, mArtModel);
            }
        });
        LinearLayout shareQqRingLayout = context.findViewById(R.id.ll_share_qq_ring);
        shareQqRingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("qq ring share ");
                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                QQShareFunction.getInstance().shareArtPicAndMsgToQQQzone((ArtDetaileActivity) mContext, mArtModel);
            }
        });

        LinearLayout shareWeiXinCollectLayout = context.findViewById(R.id.ll_share_weixin_collect);
        shareWeiXinCollectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin collect share ");
                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                String artName = "《智慧安卓》 " + mArtModel.artName;
                String artFalg = mArtModel.artFalg;

                String artImageUrl = mArtModel.artImageUrl;
                artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

                String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                WinxinSharModel winxinSharModel = new WinxinSharModel();

                winxinSharModel.webUrl = htmlUrl;
                winxinSharModel.description = artFalg;
                winxinSharModel.title = artName;

                WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.COLLE);
            }
        });


        LinearLayout artShareBottomLinearLayout = context.findViewById(R.id.ll_art_bottom_share);

        artShareBottomLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("show share ");
                showSharePopuwindow(view);
            }
        });
        LinearLayout addMyCollectLinearLayout = context.findViewById(R.id.ll_botton_art_my_colect);
        mAddMyCollectImageView = context.findViewById(R.id.iv_botton_art_my_colect);
        addMyCollectLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        ScaleAnimation scaleAnimation = new ScaleAnimation(1.2f, 1f, 1.2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAnimation.setDuration(200);
                        scaleAnimation.setInterpolator(new LinearInterpolator());
                        mAddMyCollectImageView.startAnimation(scaleAnimation);
                        addArtModelToMyColocet();
                    }
                }, mDelayTime);

            }
        });
    }

    //-----分享————————————

    private void showSharePopuwindow(View view) {
        if (mSharePopupWindow != null) {
            mSharePopupWindow.dismiss();
            mSharePopupWindow = null;
            return;
        }
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        View popupWindow_view = View.inflate(view.getContext(), R.layout.pop_share_layout, null);

        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
        mSharePopupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        //焦点设置
        mSharePopupWindow.setFocusable(true);
        //设置点击背景不消失
        mSharePopupWindow.setOutsideTouchable(true);
        // 设置透明背景
        mSharePopupWindow.setBackgroundDrawable(new ColorDrawable(
                Color.TRANSPARENT));
        //设置动画

        mSharePopupWindow.setAnimationStyle(com.studyyoun.android.androidcommonbaselibrary.R.style.popwin_from_bottom_anim_style);

        mSharePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        //显示
        mSharePopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);


        TextView cancleShareTextView = popupWindow_view.findViewById(R.id.tv_cancle_share);
        cancleShareTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSharePopupWindow.dismiss();
                mSharePopupWindow = null;
            }
        });

        LinearLayout shareWeiXinMsgLayout = popupWindow_view.findViewById(R.id.ll_share_weixin_msg_bot);
        shareWeiXinMsgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin msg share ");
                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        String artName = "《智慧安卓》 " + mArtModel.artName;
                        String artFalg = mArtModel.artFalg;

                        String artImageUrl = mArtModel.artImageUrl;
                        artImageUrl = HttpUrlHelper.BaseHost + artImageUrl;

                        String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                        WinxinSharModel winxinSharModel = new WinxinSharModel();

                        winxinSharModel.webUrl = htmlUrl;
                        winxinSharModel.description = artFalg;
                        winxinSharModel.title = artName;

                        WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.MSG);
                    }
                }, mDelayTime);


            }
        });

        LinearLayout shareWeiXinRingLayout = popupWindow_view.findViewById(R.id.ll_share_weixin_ring_bot);
        shareWeiXinRingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin ring share ");
                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        String artName = "《智慧安卓》 " + mArtModel.artName;
                        String artFalg = mArtModel.artFalg;

                        String artImageUrl = mArtModel.artImageUrl;
                        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

                        String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                        WinxinSharModel winxinSharModel = new WinxinSharModel();

                        winxinSharModel.webUrl = htmlUrl;
                        winxinSharModel.description = artFalg;
                        winxinSharModel.title = artName;

                        WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.FIND);
                    }
                }, mDelayTime);


            }
        });

        LinearLayout shareQqMsgLayout = popupWindow_view.findViewById(R.id.ll_share_bottom_qq_msg);
        shareQqMsgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("qq msg share ");
                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        QQShareFunction.getInstance().shareArtPicAndMsgToQQ((ArtDetaileActivity) mContext, mArtModel);
                    }
                }, mDelayTime);

            }
        });
        LinearLayout shareQqRingLayout = popupWindow_view.findViewById(R.id.ll_share_qq_ring_bot);
        shareQqRingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("qq ring share ");
                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        QQShareFunction.getInstance().shareArtPicAndMsgToQQQzone((ArtDetaileActivity) mContext, mArtModel);
                    }
                }, mDelayTime);


            }
        });

        LinearLayout shareWeiXinCollectLayout = popupWindow_view.findViewById(R.id.ll_share_weixin_collect);
        shareWeiXinCollectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.d("wei xin collect share ");

                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        String artName = "《智慧安卓》 " + mArtModel.artName;
                        String artFalg = mArtModel.artFalg;

                        String artImageUrl = mArtModel.artImageUrl;
                        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

                        String htmlUrl = HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                        WinxinSharModel winxinSharModel = new WinxinSharModel();

                        winxinSharModel.webUrl = htmlUrl;
                        winxinSharModel.description = artFalg;
                        winxinSharModel.title = artName;

                        WeixinShareFunction.getInstance().shareWebFunction(winxinSharModel, WeixinShareFunction.ShareType.COLLE);
                    }
                }, mDelayTime);


            }
        });

        LinearLayout copyLoationLinearLayout = popupWindow_view.findViewById(R.id.ll_copy_location);
        copyLoationLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }
                String artName = "《智慧安卓》 " + mArtModel.artName + "  点击链接查看详情  " + HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                // 从API11开始android推荐使用android.content.ClipboardManager
                // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);

                // 将文本内容放到系统剪贴板里。
                cm.setText(artName);
                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.show("已复制文章信息到剪切版", mContext);
                    }
                }, mDelayTime);

            }
        });

        //添加到我的收藏

        LinearLayout addMyCollectLinearLayout = popupWindow_view.findViewById(R.id.ll_my_collect);
        addMyCollectLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mArtModel == null) {
                    ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                    return;
                }

                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        addArtModelToMyColocet();
                    }
                }, mDelayTime);

            }
        });

        LinearLayout otherShareLayout = popupWindow_view.findViewById(R.id.ll_share_other);
        otherShareLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                closeSharePop();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mArtModel == null) {
                            ToastUtils.show("暂无文章详情，请重新进入 ", mContext);
                            return;
                        }
                        String artName = "《智慧安卓》 " + mArtModel.artName + "  点击链接查看详情  " + HttpUrlHelper.sArtShareUrl + "?id=" + mArtModel.id;

                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_TEXT, artName);
                        shareIntent.setType("text/plain");
                        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(shareIntent);
                    }
                }, mDelayTime);
            }
        });
        LinearLayout otherBackgroundLayout = popupWindow_view.findViewById(R.id.ll_share_background_other);
        otherBackgroundLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeSharePop();
            }
        });

    }

    /**
     * 添加文章至我的收藏
     */
    private void addArtModelToMyColocet() {

        UserModel userModel = CommonUserHelper.getUserModel();
        if (userModel != null) {
            if (mArtModel.articlCollectModels != null) {
                if (mArtModel.articlCollectModels.contains(String.valueOf(userModel.getUserId()))) {
                    ToastUtils.show("已收藏", mContext);
                } else {
                    commitAddArtModelToMyColocet();
                }
            } else {
                commitAddArtModelToMyColocet();
            }
        } else {
            ToastUtils.show("请登录", mContext);
        }


    }

    /**
     * 提交数据
     */
    private void commitAddArtModelToMyColocet() {

        String url = HttpUrlHelper.articleCollectUrl;
        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("artId", String.valueOf(mArtModel.getId()));
        key.put("userId", CommonUserHelper.getUserModel().getUserId() + "");

        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack() {
            @Override
            public void onSuccess(Object obj) {
                ToastUtils.show("收藏成功 ", mContext);
                List<String> articlPariseModels = mArtModel.articlCollectModels;
                if (articlPariseModels != null) {
                    articlPariseModels.add(CommonUserHelper.getUserModel().getUserId() + "");
                } else {
                    List<String> list = new ArrayList<String>();
                    list.add(CommonUserHelper.getUserModel().getUserId() + "");
                    mArtModel.articlCollectModels = list;
                }
                ArtSqlDBHelper mArtSqlDBHelper = new ArtSqlDBHelper(mContext);
                mArtSqlDBHelper.insertArt(mArtModel);

                mAddMyCollectImageView.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.my_cllect_icon4));
            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("收藏失败", mContext);
            }
        });
    }


    private long mDelayTime = 300;

    private void closeSharePop() {
        if (mSharePopupWindow != null) {
            mSharePopupWindow.dismiss();
            mSharePopupWindow = null;
        }
    }


    public void setArtmodel(ArtModel pArtmodel) {
        mArtModel = pArtmodel;
    }
}
